/*
 * Copyright 2025-2026 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package glz.hawk.poc.springboot.devlopment;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * This class is responsible for 获取环境变量
 *
 * @author Zhang Peng
 */
@RestController
@RequestMapping("/env")
@EnableConfigurationProperties
public class EnvironmentController {

    @Autowired
    private Environment env;

    @Autowired
    private ApplicationContext applicationContext;

    @GetMapping("/")
    public String home() {
        return "家和万事兴";
    }


    /**
     * 列出所有生效的环境变量
     */
    @GetMapping("list")
    public String list() {
        System.out.println("======= All Effective Property Values =======");

        // 收集所有唯一属性名
        Map<String, Object> allProperties = new HashMap<>();
        ((AbstractEnvironment) env).getPropertySources().forEach(propertySource -> {
            if (propertySource instanceof org.springframework.core.env.MapPropertySource) {
                Arrays.stream(((org.springframework.core.env.MapPropertySource) propertySource)
                        .getPropertyNames())
                    .forEach(prop -> allProperties.putIfAbsent(prop, env.getProperty(prop)));
            }
        });

        StringBuilder sb = new StringBuilder();

        // 按属性名排序输出
        new TreeMap<>(allProperties).forEach((key, value) -> {
            sb.append(key);
            sb.append(" = ");
            sb.append(value);
            sb.append("<br>");
            System.out.println(key + " = " + value);
        });
        return sb.toString();
    }

    /**
     * 列出所有的@ConfigurationProperties注解定义的bean
     */
    @GetMapping("list2")
    public String test() {
        Map<String, Object> beans = applicationContext.getBeansWithAnnotation(ConfigurationProperties.class);

        StringBuilder sb = new StringBuilder();
        beans.forEach((name, bean) -> {
            ConfigurationProperties annotation = applicationContext.findAnnotationOnBean(name,ConfigurationProperties.class);
            sb.append("Bean Name: ").append(name).append("<br>");
            sb.append("BClass: ").append(bean.getClass().getName()).append("<br>");
            sb.append("Prefix: ").append(annotation.prefix()).append("<br>");
            sb.append("<br>");
        });
        return sb.toString();
    }


}
